#include "include/RequestNetWrapper.h"

#include "core/sharedata/login_data.h"
#include "include/SSQBModel.h"
#include "include/rdProto.h"

namespace qb {

void AddRegMsgHead(const SSRequestMsg& stReq, qb::proto::Message& msg) {
  if (stReq.m_Total <= 1) {
    return;
  }

  qb::proto::VariantMap HeadMap;
  HeadMap.setInt16("total", stReq.m_Total);
  HeadMap.setInt16("idx", stReq.m_CurIdx);

  msg.setMap("ReqMsgHead", HeadMap);
}

int WrapRequestQpid(void* pIn, char* pOut) {
  if (!pIn || !pOut) return -1;

  bool bIsJava = false;
  SSRequestMsg* pReq = (SSRequestMsg*)pIn;
  if (!pReq->m_RequestID) {
    pReq->m_RequestID = GetRequestID();
  }

  return WrapRequestMsg(*pReq, pOut, Y_RD_SERVICE_MESSAGE_BLOCK_LEN,
                        bIsJava);  // 封装Message Buf
}

int WrapRequestMsg(const SSRequestMsg& stReq, char* buf, int buflen,
                   bool& bIsJava) {
  qb::proto::Message msg;  // 总的Message
  char message_type[128] = "";
  bool bDefalutSetMsgType = false;
  AddRegMsgHead(stReq, msg);
  switch (stReq.m_FuncID) {
    case E_FID_QB_CDC_AUTHORITY_REQ:     // 50030,			// 请求估值权限
    case E_FID_QB_CDC_AUTHORITY_REQ_V2:  // = 50340,
                                         // //请求中债估值权限，V3.6后替换原56030消息
    {
      sprintf(message_type, "%d", stReq.m_FuncID);
      msg.SetType(message_type);
      break;
    } break;
    case E_FID_QB_SEND_CLIENT_INFO: {
      char message_type[128] = "";
      QBSendClientInfo_t* pdata = (QBSendClientInfo_t*)stReq.m_pRequest;
      qb::proto::VariantMap submsg;
      submsg.SetString("PriceFlag", pdata->m_PriceFlag);
      submsg.setString("VersionLable", pdata->m_VersionLable);
      submsg.setString("CompanyID", pdata->m_CompanyID);
      submsg.setString("CpuParam", pdata->m_CpuParam);
      submsg.setString("MemSize", pdata->m_MemSize);
      submsg.setString("OSVersion", pdata->m_OSVersion);
      submsg.setString("PowerPlanName", pdata->m_PowerPlanName);
      submsg.setString("TimeSpanScreenShutDown",
                       pdata->m_TimeSpanScreenShutDown);
      submsg.setString("TimeSpanSleep", pdata->m_TimeSpanSleep);
      submsg.setString("ServerIPnPort", pdata->m_ServerIPnPort);
      msg.SetMap("QBSendClientInfo", submsg);
      sprintf(message_type, "%d", stReq.m_FuncID);
      msg.SetType(message_type);
    } break;
    case E_FID_QB_BEST_PRICE_REQ: {
      qb::proto::Message::List company_id_list;
      qb::proto::VariantMap submsg;
      QBBestPriceReq_t* pdata = (QBBestPriceReq_t*)stReq.m_pRequest;
      msg.setString("UserName",
                    loginData::instance().UserInfo().strAccount.c_str());
      msg.setString("IMQ_RequestType", "BONDOFFER_V2");

      for (std::list<xQBBestPriceItem_t>::const_iterator it =
               pdata->m_List.begin();
           it != pdata->m_List.end(); ++it) {
        submsg.clear();
        submsg.SetString("IMQ_CompanyID", it->m_BrokerID);
        submsg.SetInt32("IMQ_VersionFrom", it->m_VersionFrom);
        submsg.SetInt32("IMQ_VersionTo", it->m_VersionTo);
        company_id_list.push_back(submsg);
      }
      if (company_id_list.size() <= 0) return 0;
      msg.setList("IMQ_CompanyID", company_id_list);
      sprintf(message_type, "%d", stReq.m_FuncID);
      msg.SetType(message_type);
    } break;
    case E_FID_QB_INDICATIVE_PRICE_PUSH:
    case E_FID_QB_INDICATIVE_PRICE_PUSH_CANCEL:
    case E_FID_QB_BROKER_BESTPRICE_REGISTER:  //= 50136, //
                                              //经纪商行情-最优报价注册
    case E_FID_QB_BROKER_BESTPRICE_UNREGISTER:  //= 50137, //
                                                //经纪商行情-最优报价取消注册
    case E_FID_QB_BROKER_DEAL_REGISTER:    //= 50138,			//
                                           //经纪商行情-成交注册
    case E_FID_QB_BROKER_DEAL_UNREGISTER:  // = 50139,			//
                                           // 经纪商行情-成交取消注册
    {
      QBBrokerPriceRegReq_t* pdata = (QBBrokerPriceRegReq_t*)stReq.m_pRequest;
      qb::proto::VariantMap submsg;
      submsg.SetString("UserId", pdata->m_UserId);
      if (pdata->m_ver > 0) {
        submsg.SetInt32("ver", pdata->m_ver);
      }
      qb::proto::Message::List company_id_list;
      for (std::list<xQBTradeCmpyInfo_c>::const_iterator it =
               pdata->m_List.begin();
           it != pdata->m_List.end(); ++it) {
        qb::proto::VariantMap listmsg;
        listmsg.SetInt32("Id", it->m_Id);
        listmsg.SetString("Code", it->m_Code);

        company_id_list.push_back(listmsg);
      }
      if (company_id_list.size() <= 0)
        return 0;  // 无权限，不请求，add by lance 20170725
      submsg.SetList("List", company_id_list);

      msg.SetMap("xQBBrokerPriceRegReq", submsg);
      sprintf(message_type, "%d", stReq.m_FuncID);
      msg.SetType(message_type);
    } break;
    case E_FID_QB_AUTHORITY_QUERY_NEW_REQ:  // 50155,			// QB
                                            // 权限查询 tong,新的权限查询
    case E_FID_QB_USER_PERMISSION_REQ:  //= 50149,			// QB
                                        //查询 Permission 权限，king
    {
      QBAuthorityQueryReq_t* pdata = (QBAuthorityQueryReq_t*)stReq.m_pRequest;
      qb::proto::VariantMap submsg;
      submsg.SetString("UserId", pdata->m_UserId);
      submsg.SetString("CompanyId", pdata->m_CompanyId);
      msg.SetMap("xQBAuthorityQueryReq", submsg);
      sprintf(message_type, "%d", stReq.m_FuncID);
      msg.SetType(message_type);

    } break;
    case E_FID_QB_CSI_AUTHORITY_REQ: {
      qb::proto::VariantMap submsg;
      xCSIAuthorityReq_c* pdata = (xCSIAuthorityReq_c*)stReq.m_pRequest;
      submsg.setString("UserId", pdata->m_UserId);
      submsg.setString("CompanyId", pdata->m_CompanyId);
      msg.SetMap("xCSIAuthorityReq", submsg);
      sprintf(message_type, "%d", stReq.m_FuncID);
      msg.SetType(message_type);
      break;
    }
    default:
      return -1;
  }

  char message_id[128];
  if (bDefalutSetMsgType) {
    sprintf(message_type, "%d", stReq.m_FuncID);
    msg.SetType(message_type);
  }
  // 新增全局语言字段，Add by LanceZhao 2015.2.26
  msg.SetString("Language", "CHS");  //"EN","CHS","CHT"
  sprintf(message_id, "%ld", stReq.m_RequestID);
  msg.setMessageID(message_id);  ////

  int length = msg.encode(buf, buflen);
  return length;
}

int GetRequestID() {
  static int sReq = 10000001;
  return ++sReq;
}

}  // namespace qb
